﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using SuperJ.CMS.Commons;
using SuperJ.CMS.Models;
using SuperJ.CMS.Interfaces.Repositories;
using SuperJ.CMS.Interfaces.Services;
using SuperJ.CMS.ViewModels;
namespace SuperJ.CMS.Services
{
    public class AnnounceService : CMSService, IAnnounceService
    {
        //private 
        private IAnnounceRepository<SuperJ_Announce> announceRepository;
        public AnnounceService(IAnnounceRepository<SuperJ_Announce> ar)
        {
            this.announceRepository = ar;
        }
        public void Create(ViewModels.AnnounceViewModel avm)
        {
            SuperJ_Announce sj_Announce = ConvertToEntity(avm);

            announceRepository.Create(sj_Announce);

        }
        public IEnumerable<AnnounceViewModel> AnnounceList()
        {
            return new List<AnnounceViewModel>();
        }
        public IEnumerable<AnnounceViewModel> AnnounceList(string sortName,bool desc,int page ,int  pageSize)
        {
            List<AnnounceViewModel> list =new List<AnnounceViewModel>();
            //IQueryable<SuperJ_Announce> iqA = announceRepository
            //IQueryable<SuperJ_Announce> iqA = announceRepository.OrderBy("CreateTime", false);
            IQueryable<SuperJ_Announce> iqA = announceRepository.PageQuery(sortName, false, page, pageSize);
            //announceRepository.Find(c =>( c.AdminId == 1 && c.AdminName == "2"));
            foreach (SuperJ_Announce sa in iqA)
            {
                list.Add(ConvertToViewModel(sa));
            }
            return list;
        }
        public JsonResult PageAnnounceList(int page,int pageSize,string sortField,bool desc)
        {
            int count = announceRepository.Count();
            IQueryable<AnnounceViewModel> iqA = AnnounceList("CreateTime", false, page, pageSize) as IQueryable<AnnounceViewModel>;
            return JqGridHelper.GetJson<AnnounceViewModel>(iqA, "AnnounceId",page,pageSize,count);
            

        }
        public IEnumerable<AnnounceViewModel> PageAnnounceList()
        {
            List<AnnounceViewModel> list = new List<AnnounceViewModel>();
            IQueryable<SuperJ_Announce> iqA = announceRepository.PageQuery("CreateTime", false,1,2);
            foreach (SuperJ_Announce sa in iqA)
            {
                list.Add(ConvertToViewModel(sa));
            }
            return list;
        }
        public object JsonPageJqGridAnnounceList(int page,int pageSize,string orderField,bool desc)
        {
            List<AnnounceViewModel> list = new List<AnnounceViewModel>();
            IQueryable<SuperJ_Announce> iqA = announceRepository.PageQuery("CreateTime", desc, page, pageSize);
            foreach (SuperJ_Announce sa in iqA)
            {
                list.Add(ConvertToViewModel(sa));
            }
            int totalRecords = announceRepository.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
            var temp = new
            {

                total = totalPages,
                page = page,
                records = totalRecords,
                rows =list.Select(i=>new 
                {
                    id=i.AnnounceId,
                    cell=new object[]{ i.AnnounceId,i.Title,string.IsNullOrEmpty(i.LinkUrl)?false:true,i.IsPass,i.IsTop,i.CreateTime}
                })
            };
            return temp;


        }
        private AnnounceViewModel ConvertToViewModel(SuperJ_Announce sa)
        {
            AnnounceViewModel avm = new AnnounceViewModel();
            avm.ChannelId = sa.ChannelId;
            avm.Title = sa.Title;
            avm.TitleColor = sa.TitleColor;
            avm.IsTop = sa.IsTop;
            avm.IsPass = sa.IsPass;
            avm.LinkUrl = sa.LinkUrl;
            avm.Readme = sa.Readme;
            avm.FullContent = sa.FullContent;
            avm.AdminId = sa.AdminId;
            avm.AdminName = sa.AdminName;
            avm.Readme = sa.Readme;
            avm.CreateTime = sa.CreateTime;
            return avm;
        }
        private SuperJ_Announce ConvertToEntity(AnnounceViewModel avm)
        {
            SuperJ_Announce sj_Announce = new SuperJ_Announce();
            sj_Announce.ChannelId = avm.ChannelId;
            sj_Announce.Title = avm.Title;
            sj_Announce.TitleColor = avm.TitleColor;
            sj_Announce.IsTop = avm.IsTop;
            sj_Announce.IsPass = avm.IsPass;
            sj_Announce.LinkUrl = avm.LinkUrl;
            sj_Announce.Readme = avm.Readme;
            sj_Announce.FullContent = avm.FullContent;
            sj_Announce.AdminId = avm.AdminId;
            sj_Announce.AdminName = avm.AdminName;
            sj_Announce.Readme = avm.Readme;
            sj_Announce.CreateTime = DateTime.Now;
            return sj_Announce;
        }
    }
}
